//
//  LoanAttestDetailViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/20.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "LoanAttestDetailViewController.h"
#import "PayTypeViewController.h"

@interface LoanAttestDetailViewController ()
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (weak, nonatomic) IBOutlet UILabel *attestName;
@property (weak, nonatomic) IBOutlet UILabel *attestMoney;
@property (weak, nonatomic) IBOutlet UILabel *attestDate;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property(nonatomic,strong)NSString * phoneNum;
@property (weak, nonatomic) IBOutlet UILabel *attestContent;
@property (weak, nonatomic) IBOutlet UILabel *supplyChainNum;
@property (weak, nonatomic) IBOutlet UILabel *goodsStatus;
@property (weak, nonatomic) IBOutlet UILabel *gooodsAddress;


@end

@implementation LoanAttestDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
    [self getAttestDetail];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    
    _sureButton.layer.cornerRadius = 4*VerticalRatio();
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 660), NO);
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

- (IBAction)call:(id)sender {
    [Common callPhone:_phoneNum view:self.view];
}

#pragma  mark   === 确认受理/去放款
- (IBAction)giveMoney:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    
    [dic setObject:_loanId forKey:@"fappId"];
    
    self.shareApi.delegate=self;
    if ([sender.titleLabel.text isEqualToString:@"确认受理"]) {
        [self.shareApi  requestOperationWithDic:dic withPath:GS_updateCompanyApplyForLoan withHttpType:@"POST" withTag:GS_GS_updateCompanyApplyForLoan];
    }else{
        [self.shareApi  requestOperationWithDic:dic withPath:GS_saveFinacialApplyOrder withHttpType:@"POST" withTag:GS_GS_updateCompanyApplyForLoan];

    }
    
    

    
   
 
}

#pragma mark === 获取受理贷款详情
- (void)getAttestDetail{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    
    [dic setObject:_loanId forKey:@"fappId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryCompanyApplyForLoanDetail withHttpType:@"POST" withTag:GS_GS_queryCompanyApplyForLoanDetail];
    
}


#pragma mark === 天眼
- (IBAction)onlineEye:(id)sender {
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"OnlineEye"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark === 货物流向
- (IBAction)seeGoodsWay:(id)sender {
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==GS_GS_queryCompanyApplyForLoanDetail) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = database[@"result"][@"apply"];
            NSDictionary * attestDic = dic[@"memberUser"];
            
            _phoneNum = [NSString stringWithFormat:@"%@",attestDic[@"musePhone"]];
            
            _attestName.text = [NSString stringWithFormat:@"%@",attestDic[@"museUserName"]];
            _attestMoney.text = [NSString stringWithFormat:@"￥%.2f",[dic[@"fappPrice"] floatValue]];
            _attestDate.text = [NSString stringWithFormat:@"%@",dic[@"fappCreateTime"]];
            
            NSString * status = [NSString stringWithFormat:@"%@",dic[@"fappLoanStatus"]];
            if ([status isEqualToString:@"1"]) {
                status = @"已预申请";
                _supplyChainNum.text = @"暂无";
            }else if ([status isEqualToString:@"2"]) {
                status = @"已正式申请(待受理)";
            }else if ([status isEqualToString:@"3"]) {
                status = @"已受理";
                _sureButton.hidden = YES;
                [_sureButton setTitle:@"去放款" forState:UIControlStateNormal];
            }else if ([status isEqualToString:@"4"]) {
                status = @"已放款";
                _sureButton.hidden = YES;
                _scroView.contentSize = flexibleSize(CGSizeMake(0, 580), NO);

            }
            _statusLabel.text = status;
            _attestContent.text = [NSString stringWithFormat:@"%@",dic[@"fappContent"]];
            
            if ([[NSString stringWithFormat:@"%@",dic[@"fappLoanStatus"]] isEqualToString:@"1"]) {
                _supplyChainNum.text = @"暂无";
                _goodsStatus.text = @"暂无";
                _gooodsAddress.text = @"暂无";
            }else{
                _supplyChainNum.text =[NSString stringWithFormat:@"%@",dic[@"orderSysId"]];
                _goodsStatus.text = [NSString stringWithFormat:@"%@",dic[@"orderStatus"]];
                _gooodsAddress.text = [NSString stringWithFormat:@"%@",dic[@"orderAddress"]];
            }
        }
    }else if (tag == GS_GS_updateCompanyApplyForLoan){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [self getAttestDetail];
              [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
       
        }
    
    }else if (tag ==GS_GS_updateCompanyApplyForLoan){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
        PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
        //    page.payOrderPrice = database[@"result"][@"orderPrice"];
        //    page.payOrderName = @"中盘科技";
        //    page.payId = database[@"result"][@"orderNo"];
        //    page.orderFirstNo = database[@"result"][@"orderFirstNo"];
        page.payTypes = @"2";
        //            page.paySuccessBlock = ^(){
        //                [self backBtn];
        //            };
        [[Common getNavigation]pushViewController:page completion:^{
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
        }];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
